/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package tokenizer;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import model.Message;
import model.TokenList;

/**
 *
 * @author Danang
 */
public class TokenMessage {
    Tokenizer tokenizer;
    
    TokenList tokenlist;
    
    public TokenMessage() {
        tokenizer = new Tokenizer();
        tokenlist = new TokenList();
    }
    
    public void reinit() {
        tokenlist.reinit();
    }
    
    public void token_it(Message m) {
        // Tokenize Date
        if (!m.isDateEmpty()) {
            HashMap<String,ArrayList<Integer>> result;
            result = tokenizer.tokenize(m.getDate(), m.getDateIdx());
            Iterator iter = result.entrySet().iterator();
            Map.Entry pairs;
            String token;
            ArrayList<Integer> position;
            while (iter.hasNext()) {
                pairs = (Map.Entry) iter.next();
                token = (String) pairs.getKey();
                position = (ArrayList<Integer>) pairs.getValue();
                tokenlist.addToken_position(token, Message.DATE_FIELD, m.getID(), position);
            }
        }
        
        // Tokenizer To
        if (!m.isToEmpty()) {
            HashMap<String,ArrayList<Integer>> result;
            result = tokenizer.tokenize(m.getTo(), m.getToIdx());
            Iterator iter = result.entrySet().iterator();
            Map.Entry pairs;
            String token;
            ArrayList<Integer> position;
            while (iter.hasNext()) {
                pairs = (Map.Entry) iter.next();
                token = (String) pairs.getKey();
                position = (ArrayList<Integer>) pairs.getValue();
                tokenlist.addToken_position(token, Message.TO_FIELD, m.getID(), position);
            }
        }
        // Tokenizer From
        if (!m.isFromEmpty()) {
            HashMap<String,ArrayList<Integer>> result;
            result = tokenizer.tokenize(m.getFrom(), m.getFromIdx());
            Iterator iter = result.entrySet().iterator();
            Map.Entry pairs;
            String token;
            ArrayList<Integer> position;
            while (iter.hasNext()) {
                pairs = (Map.Entry) iter.next();
                token = (String) pairs.getKey();
                position = (ArrayList<Integer>) pairs.getValue();
                tokenlist.addToken_position(token, Message.FROM_FIELD, m.getID(), position);
            }
        }
        // Tokenizer Subject
        if (!m.isSubjectEmpty()) {
            HashMap<String,ArrayList<Integer>> result;
            result = tokenizer.tokenize(m.getSubject(), m.getSubjectIdx());
            Iterator iter = result.entrySet().iterator();
            Map.Entry pairs;
            String token;
            ArrayList<Integer> position;
            while (iter.hasNext()) {
                pairs = (Map.Entry) iter.next();
                token = (String) pairs.getKey();
                position = (ArrayList<Integer>) pairs.getValue();
                tokenlist.addToken_position(token, Message.SUBJECT_FIELD, m.getID(), position);
            }
        }
        
        // Tokenizer Body
        if (!m.isBodyEmpty()) {
            HashMap<String,ArrayList<Integer>> result;
            result = tokenizer.tokenize(m.getBody(), m.getBodyIdx());
            Iterator iter = result.entrySet().iterator();
            Map.Entry pairs;
            String token;
            ArrayList<Integer> position;
            while (iter.hasNext()) {
                pairs = (Map.Entry) iter.next();
                token = (String) pairs.getKey();
                position = (ArrayList<Integer>) pairs.getValue();
                tokenlist.addToken_position(token, Message.BODY_FIELD, m.getID(), position);
            }
        }
    }
    
    // get TokenList field tertentu
    public TokenList getTokenList() {
        return this.tokenlist;
    }
    
    public String toString() {
        return tokenlist.toString();
    }
    
}
